<template>
	<div>
    <Breadcrumb :style="{margin: '15px 0',float: 'left'}">
      <BreadcrumbItem>首页</BreadcrumbItem>
      <BreadcrumbItem>会员中心</BreadcrumbItem>
      <BreadcrumbItem>普通会员</BreadcrumbItem>
    </Breadcrumb>
    <Content class="layer-content">
      <Form inline>
        <FormItem>
          <Input style="width: 400px" v-model="keywords" search enter-button placeholder="请输入关键字" @on-search="searchGet" @submit.native.prevent/>
        </FormItem>
        <FormItem>
					<Cascader style="width: 300px" placeholder="请选择居村委" :data="regionArray" @on-change="regionSelector" :load-data="loadRegion"></Cascader>
				</FormItem>
        <FormItem>
          <Button @click="reset">重置</Button>
          <Button @click="createView" type="primary">新增</Button>
        </FormItem>
      </Form>
      <Table border :columns="columns" :data="dataGrid"></Table>
      <HigPager v-model="query" :total="pageTotal" :init="Init"></HigPager>
    </Content>
    <Modal :footer-hide="true" v-model="showCreate" title="新增" width="70%">
      <Create ref="refCreate" :callbank="callEvent"></Create>
    </Modal>
    <Modal :footer-hide="true" v-model="showUpdate" title="编辑" width="70%">
      <Update ref="refUpdate" :dataBus="dataVo" :callbank="callEvent"></Update>
    </Modal>
  </div>  
</template>
<script scope>
import Create from './create.vue'
import Update from './update.vue'
export default {
  data () {
    return {
      pageTotal: 0,
      dataGrid:[],
      regionArray: [],
      postRoleArray: [],
      keywords: null,
      showCreate: false,
      showUpdate: false,
      dataVo: {},
      query: {
        pageIndex: 1,
        pageSize: 15,
        tenant: false
      },
      columns: [
        {
          title: "村组(小区)",
          key: "villageGroupName"
        },
        {
          title: "居村委",
          key: "regionName",
        },
        {
          title: "创建时间",
          key: "postTime",
          width: 150,
          render: (h, params) => {
            return h("div", this.$date(params.row.postTime,'yyyy-MM-dd HH:mm'));
          },
        },
        {
          title: "操作",
          key: "action",
          width: 120,
          render: (h, params) => {
            return h("nav", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small",
                  },
                  style: {
                    marginLeft: "10px",
                  },
                  on: {
                    click: () => {
                      this.editView(params.row)
                    },
                  },
                },
                "编辑"
              )
            ])
          },
        },
      ]
    }
  },
  components: {
    Create,
    Update
  },
  methods: {
    editView(e){
      this.dataVo = e
      this.$refs.refUpdate.Init()
      this.showUpdate = true
    },
    createView() {
      this.$refs.refCreate.Init()
      this.showCreate = true
    },
    callEvent(){
      this.showCreate = false
      this.Init()
    },
    //获取列表
    Init(){
      new this.$rest({
        gateway: 'agrobiz',
        service: 'agrobiz-uc',
        url: '/v1.0/village/group/page',
        query: this.query,
        action: (res) => {
          if (res.data.code === 20000) {
            this.pageTotal = res.data.data.count
            this.dataGrid = res.data.data.list
          } else {
            this.$Message.error(res.data.msg)
          }
        }
      }).get()
     },
    regionInit() {
      new this.$rest({
				gateway: 'agrobiz',
        service: "agrobiz-uc",
        url: "/v1.0/plus/union/region/lazy",
				query: {
					equal: {
            regionId: "0"
          }
				},
				action: (res)=>{
					if(res.data.code === 20000){
						const data = res.data.data
						this.regionArray = data
					} else {
						this.$Message.error({
              content: res.data.msg
            })
					}
				}
			}).get()
    },
    loadRegion(item, callback) {
			item.loading = true
			new this.$rest({
				gateway: 'agrobiz',
        service: "agrobiz-uc",
        url: "/v1.0/plus/union/region/lazy",
				query: {
					equal: {
            regionId: item.id,
            regionLevel: 5
          }
				},
				action: (res)=>{
					if(res.data.code === 20000){
						const data = res.data.data
						item.children = data
						item.loading = false
            callback()
					} else {
						this.$Message.error({
              content: res.data.msg
            })
					}
				}
			}).get()
		},
    regionSelector(value) {
      this.query.equal.regionCode = value[value.length - 1]
      this.Init()
		},
     //搜索
    searchGet() {
      let sqlLike = {
        fields: ["village_group_name","village_name","region_name"],
        keyword: this.keywords,
      }
      this.query.sqlLike = sqlLike
      this.query.pageIndex = 1
      this.Init()
    },
    //重置
    reset() {
      delete this.query.sqlLike
      this.keywords = null
      this.Init()
    },
  },
  created(){
    this.Init()
    this.regionInit()
  }
}
</script>
<style lang="less" scoped>
.imgs {
  width: 80%;
}
</style>